package com.erp.activity

import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.erp.R
import com.erp.module.JiJianCaiJi.FragmentKJiJianCaiJi
import com.erp.module.ProjectOrderProcess.FragmentProjectOrderProcess
import com.erp.module.YuliaoPrint.FragmengtYuliaoPrintLabel
import com.erp.module.anZhuangZHiNan.FragmentKAnZhuangZhiNan
import com.erp.module.article.FragmentArticle
import com.erp.module.caiLiaoZhiLiang.FragmentKCaiLiaoZhiLiang
import com.erp.module.crm.FragmentCrm
import com.erp.module.dingDan.FragmentDingDan
import com.erp.module.duizhangPhoto.FragmentDuizhangPhoto
import com.erp.module.faHuoDanJu.faHuo2.FragmentFaHuo2
import com.erp.module.gongGao.FragmentGongGao
import com.erp.module.jiJian.FragmentKJiJian
import com.erp.module.jiaFang.FragmengtJiaFang
import com.erp.module.jiehao.FragmentKJieHao
import com.erp.module.jiehao.FragmentKZhiXiang
import com.erp.module.jingXiHua.FragmentJingXiHua
import com.erp.module.kaoShi.FragmentKKaoShi
import com.erp.module.oem.FragmentOem
import com.erp.module.packPiece.FragmentPackPiece
import com.erp.module.peibanPcode.FragmengtPeiban
import com.erp.module.pieceworkGrouping.FragmentPieceworkGrouping
import com.erp.module.sheJiZhiNan.FragmentSheJiZhiNan
import com.erp.module.storesRequisitionPackage.FragmentSrp
import com.erp.module.xiSuZhiXiang.FragmentXiSuZhiXiang
import com.erp.module.xianChangPaiZhao.FragmentKXianchangPaizhao
import com.erp.module.yiJianXiang.FragmentYiJianXIang
import com.erp.module.yuliaofreight.FragmentYuliaoGiFreight
import com.erp.module.zhiLiang.FragmentZhiLiang
import com.erp.module.zhichengJianyan.FragmentProcessInspection
import com.erp.module.zhuanMaiDianTu.FragmentZhuanMaiDianTu
import com.erp.myFragment.BaseKFragment
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.dialog_etshuru.FenPeiEt
import kotlinx.android.synthetic.main.dialog_etshuru.LeftBt
import kotlinx.android.synthetic.main.dialog_etshuru.RightBt
import kotlinx.android.synthetic.main.dialog_etshuru.shuruTitle
import kotlinx.android.synthetic.main.header_knoya.iv_header_left
import kotlinx.android.synthetic.main.header_knoya.tv_header_right
import kotlinx.android.synthetic.main.header_knoya.tv_header_title

class HomeActivity : BaseActivity(), View.OnClickListener {
    private lateinit var transaction: FragmentTransaction
    private lateinit var mFragmentMan: FragmentManager
    lateinit var from: BaseKFragment
    private var title = ""
    private var code = 0
    private var select: Int = 0
    private var all: String = ""

    private val a = "HomeActivity"
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_home
    }

    override fun initCreate() {
        initView()
        initData()
    }

    private fun initData() {
        mFragmentMan = supportFragmentManager
        transaction = mFragmentMan.beginTransaction()
        createFrom()
        from = createFrom()
        from.userVisibleHint = true
        transaction.add(R.id.fragment_main, from).commit()
    }

    private fun initView() {
        val intent = intent
        all = intent.getStringExtra("all") ?: ""
        select = intent.getIntExtra("select", 0)
        title = intent.getStringExtra("title") ?: ""
        code = intent.getIntExtra("code", 0)
        iv_header_left.visibility = View.VISIBLE
        tv_header_title.text = title
        iv_header_left.setOnClickListener { finish() }
        setRightHeade()
    }

    private fun setRightHeade(){
        when(code){
            180 -> {
                tv_header_right.visibility = View.VISIBLE
                tv_header_right.text = "设置"
                tv_header_right.setOnClickListener(this)
            }
        }
    }

    override fun onClick(v: View?) {
        when(v){
            tv_header_right -> {
                val printerSign = sp.getString("printerSign","").toString()
                val dialog = AlertDialog.Builder(this).create()
                dialog.setView(EditText(this))
                dialog.show()
                dialog.setContentView(R.layout.dialog_etshuru)
                val window = dialog.window
                val p = window?.attributes // 获取对话框当前的参数值
                if (p != null) {
                    p.width = (sp.getInt("width", 0) * 0.9).toInt()
                } // 宽度设置为屏幕的0.65
                if (window != null) {
                    window.attributes = p
                }
                dialog.FenPeiEt.setText(printerSign)
                dialog.shuruTitle.text = "打印机标记"
                dialog.FenPeiEt.hint = "输入打印机标记"
                dialog.LeftBt.setOnClickListener {
                    from.setData(dialog.FenPeiEt.text.toString().trim())
                    dialog.dismiss()
                }
                dialog.RightBt.setOnClickListener {
                    dialog.dismiss()
                }
            }
        }
    }



    private fun createFrom(): BaseKFragment {
        when (code) {
            10 -> {
                return FragmentFaHuo2();
            }

            20 -> {
                return FragmentKKaoShi()
            }

            30 -> {
                return FragmentCrm()
            }

            40 -> {
                return FragmentKCaiLiaoZhiLiang()
            }

            50 -> {
                return FragmentSheJiZhiNan()
            }

            60 -> {
                return FragmentKAnZhuangZhiNan()
            }

            70 -> {
                val from = FragmentDingDan()
                from.setSelect(select)
                from.setUpdate(all)
                return from
            }

            80 -> {
                return FragmentZhiLiang()
            }

            90 -> {
                return FragmentPackPiece()
            }

            100 -> {
                return FragmentYiJianXIang()
            }

            110 -> {
                return FragmentProjectOrderProcess()
            }

            120 -> {
                return FragmentZhuanMaiDianTu()
            }

            130 -> {
                val from = FragmentKXianchangPaizhao()
                Myutil.Mylog("sousuo",all)
                from.isTiao = true
                from.setSousuo(all)
                return from
            }

            140 -> {
                return FragmentArticle()
            }

            150 -> {
                return FragmentOem()
            }

            160 -> {
                return FragmentDuizhangPhoto()
            }

            170 -> {
                return FragmentYuliaoGiFreight()
            }

            180 -> {
                return FragmentPieceworkGrouping()
            }
            190 -> {
                return FragmengtYuliaoPrintLabel()
            }
            200 -> {
                return FragmentOem()
            }
            210 -> {
                return FragmentProcessInspection()
            }
            220 -> {
                return FragmentJingXiHua()
            }
            230 -> {
                return FragmengtJiaFang()
            }
            240 -> {
                return FragmentXiSuZhiXiang()
            }
            250 -> {
                return FragmentKZhiXiang()
            }
            260 -> {
                return FragmentKJieHao()
            }
            270 -> {
                return FragmentKJiJianCaiJi()
            }
            280 -> {
                return FragmentKJiJian()
            }
            290 -> {
                return FragmengtPeiban()
            }
            300 -> {
                return FragmentSrp()
            }
            else ->{
                return FragmentGongGao()
            }
        }
    }


}